using System.Diagnostics;
using System.Text;

//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.nioneo.store
{

	public class DynamicRecord : AbstractRecord
	{
		private sbyte[] data = null;
		private char[] charData = null;
		private int length;
		private int prevBlock = Record.NO_PREV_BLOCK.intValue();
		private int nextBlock = Record.NO_NEXT_BLOCK.intValue();
		private bool isLight = false;
		private int type;

		public DynamicRecord(int id) : base(id)
		{
		}

		public virtual int getType()
		{
			return type;
		}

		internal virtual void setType(int type)
		{
			this.type = type;
		}

		internal virtual void setIsLight(bool status)
		{
			this.isLight = status;
		}

		public virtual bool isLight()
		{
			return isLight;
		}

		public virtual int Length
		{
			set
			{
				this.length = value;
			}
			get
			{
				return length;
			}
		}

		public override void setInUse(bool inUse)
		{
			base.setInUse(inUse);
			if (!inUse)
			{
				data = null;
			}
		}

		public virtual void setInUse(bool inUse, int type)
		{
			this.type = type;
			this.setInUse(inUse);
		}

		public virtual void setData(sbyte[] data)
		{
			isLight = false;
			this.length = data.Length;
			this.data = data;
		}

		public virtual void setCharData(char[] data)
		{
			isLight = false;
			this.length = data.Length * 2;
			this.charData = data;
		}


		public virtual sbyte[] getData()
		{
			Debug.Assert(!isLight);
			Debug.Assert(charData == null);
			return data;
		}

		public virtual bool isCharData()
		{
			return charData != null;
		}

		public virtual char[] getDataAsChar()
		{
			Debug.Assert(!isLight);
			Debug.Assert(data == null);
			return charData;
		}

		public virtual int getPrevBlock()
		{
			return prevBlock;
		}

		public virtual void setPrevBlock(int prevBlock)
		{
			this.prevBlock = prevBlock;
		}

		public virtual int getNextBlock()
		{
			return nextBlock;
		}

		public virtual void setNextBlock(int nextBlock)
		{
			this.nextBlock = nextBlock;
		}

		public override string ToString()
		{
			StringBuilder buf = new StringBuilder();
			buf.Append("DynamicRecord[").Append(getId()).Append(",").Append(inUse());
			if (inUse())
			{
				buf.Append(",").Append(prevBlock).Append(",").Append(isLight ? null : data.Length).Append(",").Append(nextBlock).Append("]");
			}
			return buf.ToString();
		}
	}
}